using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Common.Security;
using Microsoft.ConnectionEngine.Common;

namespace Microsoft.ConnectionEngine.Administration.Core.ServiceProvider.Business
{
    public class PoolServiceProviderTypeManager
    {
        public static PoolServiceProviderTypes PoolServiceProviderTypeLinkSearch(HSDPrincipal currentPrincipal, string poolID, string serviceProviderTypeID)
        {
            if (!currentPrincipal.IsInRole(RoleTypes.Administrator))
                throw new SecurityException();

            string content = string.Format("<{0}>{1}</{0}><{2}>{3}</{2}>", "PoolID", poolID, "ServiceProviderTypeID", serviceProviderTypeID);
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "PoolServiceProviderTypeSearch", content);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1061, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return (PoolServiceProviderTypes)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(PoolServiceProviderTypes));
        }


        /// <summary>
        /// Place a ServiceProviderType into a Pool
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="poolID"></param>
        /// <param name="serviceProviderTypeID"></param>
        /// <returns></returns>
        public static bool PoolServiceProviderTypeLinkInsert(HSDPrincipal currentPrincipal, int poolID, int serviceProviderTypeID)
        {               
            PoolServiceProviderTypeLink poolSptLink = new PoolServiceProviderTypeLink();
            poolSptLink.PoolID = poolID;
            poolSptLink.ServiceProviderTypeID = serviceProviderTypeID;

            return PoolServiceProviderTypeLinkInsert(currentPrincipal, poolSptLink);
        }

        public static bool PoolServiceProviderTypeLinkInsert(HSDPrincipal currentPrincipal, PoolServiceProviderTypeLink poolSptLink)
        {
            string poolSptLinkXml = AdapterHelper.ConstructObjectXml(poolSptLink, typeof(PoolServiceProviderTypeLink));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, poolSptLinkXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1029, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);
        }

        /// <summary>
        /// Place a list of ServiceProviderTypes into a Pool
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="poolsptLinks"></param>
        /// <returns></returns>
        public static bool PoolServiceProviderTypeLinksInsert(HSDPrincipal currentPrincipal, PoolServiceProviderTypeLinks poolsptLinks)
        {          
            string poolsptLinksXml = AdapterHelper.ConstructObjectXml(poolsptLinks, typeof(PoolServiceProviderTypeLinks));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, poolsptLinksXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1063, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return AdapterHelper.GetBooleanFromResponse(result);
        }

        /// <summary>
        /// Remove a ServiceProviderType from a Pool
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="poolID"></param>
        /// <param name="serviceProviderTypeID"></param>
        /// <returns></returns>
        public static bool PoolServiceProviderTypeLinkDelete(HSDPrincipal currentPrincipal, int poolID, int serviceProviderTypeID)
        {          
            PoolServiceProviderTypeLink poolSptLink = new PoolServiceProviderTypeLink();
            poolSptLink.PoolID = poolID;
            poolSptLink.ServiceProviderTypeID = serviceProviderTypeID;

            return PoolServiceProviderTypeLinkDelete(currentPrincipal, poolSptLink);
        }


        /// <summary>
        /// Remove a ServiceProviderType from a Pool
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="PoolServiceProviderTypeLink"></param>        
        /// <returns></returns>
        public static bool PoolServiceProviderTypeLinkDelete(HSDPrincipal currentPrincipal, PoolServiceProviderTypeLink poolSptLink)
        {
            string poolSptLinkXml = AdapterHelper.ConstructObjectXml(poolSptLink, typeof(PoolServiceProviderTypeLink));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, poolSptLinkXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1028, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);
        }


        /// <summary>
        /// Place a list of ServiceProviderTypes into a Pool
        /// </summary>
        /// <param name="currentPrincipal"></param>
        /// <param name="poolsptLinks"></param>
        /// <returns></returns>
        public static bool PoolServiceProviderTypeLinksDelete(HSDPrincipal currentPrincipal, PoolServiceProviderTypeLinks poolsptLinks)
        {         
            string poolsptLinksXml = AdapterHelper.ConstructObjectXml(poolsptLinks, typeof(PoolServiceProviderTypeLinks));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, poolsptLinksXml);

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1068, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return AdapterHelper.GetBooleanFromResponse(result);
        }
    }
}
